package edu.yctc.manage.controller.rest;

import edu.yctc.manage.entity.CourseDO;
import edu.yctc.manage.entity.PageDO;
import edu.yctc.manage.entity.ResultDO;
import edu.yctc.manage.service.CourseService;
import edu.yctc.manage.utils.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @ClassName CourseRestController
 * @Description TODO
 * @Author zyj
 * @Date 2021/3/31 14:51
 * @Version 1.0
 **/
@RestController
@RequestMapping("manage/course")
public class CourseRestController {

    @Autowired
    private CourseService courseService;

    @RequestMapping("/get-vo-list")
    public ResultDO getVOList(@RequestParam Map<String, Object> params) {
        PageDO page = courseService.getVOList(new PageQuery(params));
        return ResultDO.ok().put("page", page);
    }

    @PostMapping("/save")
    public ResultDO save(@RequestParam("courseName") String name, @RequestParam("sdeptName") String sdeptName) {
        CourseDO courseDO = new CourseDO();
        courseDO.setName(name);
        courseDO.setSdeptId(Long.parseLong(sdeptName));
        int result = courseService.save(courseDO);
        if (result == 0) {
            return ResultDO.error();
        }
        return ResultDO.ok();
    }

    @PostMapping("/update")
    public ResultDO update(@RequestParam("id") String id, @RequestParam("courseName") String name,
        @RequestParam("sdeptName") String sdeptName) {
        CourseDO courseDO = new CourseDO();
        courseDO.setName(name);
        courseDO.setSdeptId(Long.parseLong(sdeptName));
        courseDO.setId(Long.parseLong(id));
        int result = courseService.update(courseDO);
        if (result == 0) {
            return ResultDO.error();
        }
        return ResultDO.ok();
    }

    @PostMapping("/delete")
    public ResultDO delete(@RequestBody Long[] ids) {
        int result = courseService.delete(ids);
        if (result == 0) {
            return ResultDO.error();
        }
        return ResultDO.ok();
    }

}
    